﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LumenLiquid.Debug;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LumenLiquid.InGame.world
{
    public sealed class Skybox
    {
        private Model _skycube;
        private TextureCube _texture;
        private Effect _effect;
        private float _size = 50f;
        private Matrix _world;

        public bool Init(string texture)
        {
            DebugHandler.Log("Initializing Skybox...", DebugHandler.EDebugLevel.Code);

            _texture = Main.Instance.Texturemanager.GetWorldTextureCube(texture);
            _skycube = Main.Instance.Modelmanager.GetGroupModel(Path.Ressources.SkyboxModel, Path.Ressources.InGameGroup);
            _effect = Main.Instance.Effectmanager.GetWorldEffect(Path.Ressources.SkyboxEffect); //TODO: single effect for each skybox
            _world = Matrix.CreateScale(_size);

            _effect.Parameters["SkyBoxTexture"].SetValue(_texture);
            _effect.Parameters["Projection"].SetValue(MainGame.Instance.Projection);

            return true;
        }

        public void Update(GameTime gameTime)
        {
            _world = Matrix.CreateScale(_size) * Matrix.CreateTranslation(MainGame.Instance.CameraPosition);

            _effect.Parameters["World"].SetValue(_world);
            _effect.Parameters["View"].SetValue(MainGame.Instance.Camera);
            _effect.Parameters["CameraPosition"].SetValue(MainGame.Instance.CameraPosition);
        }

        public void Draw()
        {
            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {
                foreach (ModelMesh mesh in _skycube.Meshes)
                {
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        part.Effect = _effect;
                    }

                    mesh.Draw();
                }
            }
        }
    }
}
